//写接口
const express = require('express');
const db = require('../db/dataDB')
const router = express.Router();
//查询所有书籍
router.get('/getbooks', (req, res) => {
    let sql = 'select * from bks';
    db.query(sql, (err, results) => {
        if (err) return console.log(err.message);

        res.send({
            "status": 200,
            "msg": "获取图书列表成功",
            data: results,
        });
    })
})

//post 配置数据解析器 添加图书
router.post('/addbooks', (req, res) => {
    //req.body;
    const bks = req.body;
    for (let k in bks) {
        if (bks[k] == '' || bks[k].replace(/\s*/g, "") == "") return res.send({
            "status": 500,
            "msg": "参数值不能为空！",
        });
    }

    //插入数据sql数据
    let sql = 'insert into bks (bookname,author,publisher) values (?,?,?)';
    //使用数组的形式，依次为？占位符赋予指定值
    db.query(sql, [bks.bookname, bks.author, bks.publisher], (err, results) => {
        if (err) {
            if (err.errno == 1062) {
                return res.send({
                    "status": 500,
                    "msg": "书籍重复添加",
                });
            } else if (err.errno == 1048) {
                return res.send({
                    "status": 500,
                    "msg": "参数错误！",
                });
            } else {
                return res.send({
                    "status": 500,
                    "msg": err,
                });
            }
        }
        if (results.affectedRows === 1) {
            res.send({
                "status": 201,
                "msg": "书籍增加成功！",
            });
        }
    })
})
//简单写法添加图书
router.post('/addbooks2', (req, res) => {
    //req.body;
    const bks = req.body;
    //插入数据sql数据
    let sql = 'insert into bks set ?';
    //使用数组的形式，依次为？占位符赋予指定值
    db.query(sql, bks, (err, results) => {
        if (err) return res.send(err.message);
        if (results.affectedRows === 1) {
            res.send({
                "status": 201,
                "msg": "书籍增加成功！",
            });
        }
    })
})

// 更新数据
router.post('/updbook', (req, res) => {
    let bks = req.body;
    let sql = 'update bks set bookname=?,author=?,publisher=? where id=?';
    db.query(sql, [bks.bookname, bks.author, bks.publisher, bks.id], (err, results) => {
        if (err) return res.send(err.message);
        if (results.affectedRows === 1) {
            res.send({
                "status": 202,
                "msg": "书籍修改成功！",
            });
        }
    })
})

//删除数据
router.get('/delbooks', (req, res) => {
    //获取前端返回过来的id
    let id = req.query.id;
    let sql = 'delete from bks where id=?';
    db.query(sql, [id], (err, results) => {
        if (err) return res.send({
            "status": 500,
            "msg": "删除图书失败！",
        });
        console.log(results.affectedRows);
        if (results.affectedRows == 1) {
            return res.send({
                "status": 200,
                "msg": "删除图书成功！",
            });
        } else {
            res.send({
                "status": 501,
                "msg": '删除失败'
            });
        }
    })
})

module.exports = router